﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SpringStudio.Common;
using SpringStudio.DAL;
using Sdepci.InfoCode.BLL.Authority;

namespace Sdepci.InfoCode.Web.Authority
{
    public partial class RoleEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.FuncGuids.Attributes.Add("readonly", "readonly");
                this.FuncGuids.Attributes.Add("onfocus", "javascript:this.blur();");
                this.BindData();
            }
        }

        private void BindData()
        {
            if (this.Request.QueryString["RoleGuid"] != null && this.Request.QueryString["RoleGuid"].Trim().Length > 0)
            {
                this.lblTitle.Text += "[修改]";

                Role role = new Role(this.Request.QueryString["RoleGuid"].Trim());
                this.RoleName.Text = role.RoleName;
                this.FuncGuids.Text = role.FuncGuids;
                this.Seq.Text = role.Seq.ToString();
            }
            else
            {
                this.lblTitle.Text += "[新增]";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string msg = string.Empty;
            if (this.Request.QueryString["RoleGuid"] != null && this.Request.QueryString["RoleGuid"].Trim().Length > 0)
            {
                bool result = new RoleManager().Update(this.Request.QueryString["RoleGuid"].Trim(), this.RoleName.Text.Trim(), this.FuncGuids.Text.Trim(), int.Parse(this.Seq.Text.Trim()));
                msg = result ? "角色修改成功！" : "角色修改失败！";
            }
            else
            {
                bool result = new RoleManager().Add(this.RoleName.Text.Trim(), this.FuncGuids.Text.Trim(), int.Parse(this.Seq.Text.Trim()));
                msg = result ? "角色新增成功！" : "角色新增失败！";
            }

            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">alert(\"" + msg + "\");document.location.href='RoleList.aspx';</script>");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("RoleList.aspx");
        }
    }
}
